const router = require('koa-router')()
const query = require("../db/mysql");

router.get('/paintings', async (ctx, next) => {
    try {
        let sql = `SELECT pId,pName FROM painting`
        let data = await query(sql);
        return ctx.body = {
            status: 200,
            data: data,
            msg: ''
        }
    } catch (error) {
        console.log(error);
        return ctx.body = {
            status: 500,
            msg: error
        }
    }
})
router.get('/paint', async (ctx, next) => {
    try {
        let sql = `SELECT * FROM painting where pId = ?`
        let data = await query(sql,ctx.query.pId);
        return ctx.body = {
            status: 200,
            data: data,
            msg: ''
        }
    } catch (error) {
        console.log(error);
        return ctx.body = {
            status: 500,
            msg: error
        }
    }
})
router.post('/paintings', async (ctx, next) => {
    var path = ctx.request.files.file.path;
    path = path.split("/");
    const pContent = 'images/'+ path[path.length-1];
    console.log(pContent);
    const {'画作id: ':pId, '作者id: ':aId, '画作名称: ':pName,'画作流派: ': pSchools,'画作风格: ': pStyle,'画作主题: ':pTheme,'画作朝代: ': pDynasty,'图片内容': pInformation} = ctx.request.body;
    console.log(pId);
    try {
        let sql = `insert into painting values(?,?,?,?,?,?,?,?,?)`;
        let data = await query(sql,[pId,aId,pContent,pName,pSchools,pStyle,pTheme,pDynasty,pInformation]);
        return ctx.body = {
            status: 200,
            data: '上传成功！',
            msg: ''
        }
    } catch (error) {
        console.log(error);
        return ctx.body = {
            status: 500,
            msg: error
        }
    }
})

router.post('/udpaint', async (ctx, next) => {
    var path = ctx.request.files.file.path;
    path = path.split("/");
    const pContent = 'images/'+ path[path.length-1];
    const {'画作id: ':pId, '作者id: ':aId, '画作名称: ':pName,'画作流派: ': pSchools,'画作风格: ': pStyle,'画作主题: ':pTheme,'画作朝代: ': pDynasty,'图片内容': pInformation} = ctx.request.body;
    console.log(pId);
    try {
        let sql = `update painting set aId=?,pContent=?,pName=?,pSchools=?,pStyle=?,pTheme=?,pDynasty=?,pInformation=?where pId=?`;
        let data = await query(sql,[aId,pContent,pName,pSchools,pStyle,pTheme,pDynasty,pInformation,pId]);
        return ctx.body = {
            status: 200,
            data: '修改成功！',
            msg: ''
        }
    } catch (error) {
        console.log(error);
        return ctx.body = {
            status: 500,
            msg: error
        }
    }
})


router.delete('/paintings',async(ctx,next)=>{
    try {
        let sql = `delete from painting where pId = ?`;
        let data = await query(sql, [ctx.query.pId]);
        return ctx.body = {
            status: 200,
            data: '删除成功！',
            msg: ''
        }
      } catch (error) {
          console.log(error);
        return ctx.body = {
          status: 500,
          msg: error
        }
      }

})

module.exports = router;